package com.cskaoyan.market.service.impl;

import com.cskaoyan.market.db.domain.MarketAdmin;
import com.cskaoyan.market.db.domain.MarketAdminExample;
import com.cskaoyan.market.db.mapper.MarketAdminMapper;
import com.cskaoyan.market.service.AdminAdminService;
import com.cskaoyan.market.util.MybatisUtil;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.SqlSession;

import java.time.LocalDateTime;
import java.util.List;

public class AdminAdminServiceImpl implements AdminAdminService {
    @Override
    public List<MarketAdmin> list(String page, String limit, String username, String sort, String order) {
        SqlSession session = MybatisUtil.getSession();
        MarketAdminMapper mapper = session.getMapper(MarketAdminMapper.class);

        MarketAdminExample adminExample = new MarketAdminExample();
        MarketAdminExample.Criteria criteria = adminExample.createCriteria();
        // 做逻辑删除
        criteria.andDeletedEqualTo(false);
        //做管理员名称查询
        if (!StringUtils.isEmpty(username)){
            criteria.andUsernameEqualTo(username);
        }
        // 做排序
        adminExample.setOrderByClause(sort + " " + order);
        // 分页
        PageHelper.startPage(Integer.parseInt(page), Integer.parseInt(limit));

        List<MarketAdmin> marketAdmins = mapper.selectByExampleSelective(adminExample, MarketAdmin.Column.avatar,
                MarketAdmin.Column.id, MarketAdmin.Column.roleIds, MarketAdmin.Column.username);

        session.commit();
        session.close();

        return marketAdmins;
    }

    @Override
    public void create(String password, String username, Integer[] roleIds, String avatar) {
        SqlSession session = MybatisUtil.getSession();
        MarketAdminMapper mapper = session.getMapper(MarketAdminMapper.class);
        MarketAdmin marketAdmin = new MarketAdmin();

        marketAdmin.setAvatar(new String("123456"));
        marketAdmin.setPassword(password);
       marketAdmin.setRoleIds(roleIds);
        marketAdmin.setUsername(username);

        mapper.insertSelective(marketAdmin);
        session.commit();
        session.close();

    }

    @Override
    public void update(String id, String avatar, String username, String password, Integer[] roleIds) {
        SqlSession session = MybatisUtil.getSession();
        MarketAdminMapper mapper = session.getMapper(MarketAdminMapper.class);


        MarketAdmin marketAdmin = new MarketAdmin();
        marketAdmin.setId(Integer.parseInt(id));
        marketAdmin.setAvatar(avatar);
        marketAdmin.setUsername(username);
        marketAdmin.setPassword(password);
        marketAdmin.setRoleIds(roleIds);
        marketAdmin.setUpdateTime(LocalDateTime.now());

        mapper.updateByPrimaryKeySelective(marketAdmin);

        session.commit();
        session.close();
    }

    @Override
    public void delete(String id) {
        SqlSession session = MybatisUtil.getSession();
        MarketAdminMapper mapper = session.getMapper(MarketAdminMapper.class);
        mapper.logicalDeleteByPrimaryKey(Integer.parseInt(id));
        session.commit();
        session.close();
    }
}
